//
//  YXLProduceListView.m
//  MyNewProject
//
//  Created by 于兴雷 on 2019/2/16.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "YXLProduceListView.h"
#import "XRWaterfallLayout.h"
#import "XMHistoryCollectionViewCell.h"

@interface YXLProduceListView()

@property(nonatomic,strong)UICollectionView *collectionView;

@end

static NSString *const superCollectionViewCellID = @"XMHistoryCollectionViewCell";

@implementation YXLProduceListView

-(id)init
{
    self = [super init];
    
    if (self) {
        [self initView];
    }
    
    return self;
}

-(void)initView
{
    
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        //创建瀑布流布局
        XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
        //或者一次性设置
        [waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(280, 10, 10, 10)];
        //设置代理，实现代理方法
        waterfall.delegate = self;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, IS_IPhoneX_All?88:64, SCREEN_WIDTH, SCREENH_HEIGHT - SKTabbarHeight - SKBottomSafe_Height - (IS_IPhoneX_All?88:64)) collectionViewLayout:waterfall];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[XMHistoryCollectionViewCell class] forCellWithReuseIdentifier:superCollectionViewCellID];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
        //waterfall.headerReferenceSize =CGSizeMake(SCREEN_WIDTH ,200);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        
        //
        
//        if (@available(iOS 11.0, *)) {
//            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        }else {
//            self.automaticallyAdjustsScrollViewInsets = NO;
//        }
    }
    return _collectionView;
}

#pragma -mark XRWaterfallLayoutDelegate
//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
    //根据数据 展示不同的高度
    NSString * productName = @"fd";
    CGSize lblSize = [productName boundingRectWithSize:CGSizeMake(itemWidth - 20, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    
    return lblSize.height > 20?(itemWidth * 0.6 + 100):(itemWidth * 0.6 + 80);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMHistoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:superCollectionViewCellID forIndexPath:indexPath];
    //[cell LoadProductInfomation:self.dataArray[indexPath.row]];
    return cell;
}

//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    ProductDetailViewController * pdVC = [[ProductDetailViewController alloc]init];
//    pdVC.productId = self.dataArray[indexPath.row];
//    [self.navigationController pushViewController:pdVC animated:YES];
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
